# 导包
from sklearn.datasets import make_blobs
import matplotlib.pyplot  as plt
from sklearn.cluster import KMeans
from sklearn.metrics import calinski_harabasz_score

# 构建数据
x,y=make_blobs(n_samples=1000,n_features=2,centers=[[-1,-1],[0,0],[1,1],[2,2]],
           cluster_std=[0.4,0.4,0.4,0.2],random_state=22)

plt.scatter(x[:,0],x[:,1])
plt.show()

# 聚类
model = KMeans(n_clusters=4,random_state=22)
# model.fit(x)
# y_pred=model.predict(x)
y_pred=model.fit_predict(x)

# 可视化
plt.scatter(x[:,0],x[:,1],c=y_pred)
plt.show()

# 评估
print(calinski_harabasz_score(x, y_pred))